package com.nycreativity.elecreativity.controller


import com.nycreativity.elecreativity.aop.ValidateAspect
import com.nycreativity.elecreativity.entity.ResultData
import com.nycreativity.elecreativity.entity.cons.CommonURLConstants
import com.nycreativity.elecreativity.entity.param.goodstype.GoodsTypeAddParam
import com.nycreativity.elecreativity.entity.param.goodstype.GoodsTypeUpdateParam
import com.nycreativity.elecreativity.entity.vo.goodstype.GoodsTypeVO
import com.nycreativity.elecreativity.service.GoodsTypeService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-11
 */
@RestController
@RequestMapping(CommonURLConstants.GOODS_TYPE_PREFIX)
@Api(tags = ["商品类型api"])
class GoodsTypeController{

    @Resource
    lateinit var goodsTypeService: GoodsTypeService

    @GetMapping
    @ApiOperation("根据id查询")
    fun getById(id: Long): ResultData<GoodsTypeVO> {
        val goodsTypeVO = goodsTypeService.queryById(id)
        return ResultData.SUCCESS(data = goodsTypeVO)
    }

    @GetMapping("all")
    @ApiOperation("获取全部")
    fun getAll(): ResultData<List<GoodsTypeVO>> {
        val list = goodsTypeService.queryAll()
        return ResultData.SUCCESS(data = list)
    }

    @PostMapping
    @ApiOperation("添加")
    @ValidateAspect.ParamValidate
    fun add(@RequestBody @Valid goodsTypeAddParam: GoodsTypeAddParam, bindingResult: BindingResult): ResultData<Any> {
        val res = goodsTypeService.add(goodsTypeAddParam)
        return if(res){
            ResultData.SUCCESS()
        }else{
            ResultData.FAILED("请检查参数")
        }
    }

    @PutMapping
    @ApiOperation("根据id修改")
    @ValidateAspect.ParamValidate
    fun updateById(@RequestBody @Valid goodsTypeUpdateParam: GoodsTypeUpdateParam, bindingResult: BindingResult): ResultData<Any>{
        val res = goodsTypeService.updateById(goodsTypeUpdateParam)
        return if(res){
            ResultData.SUCCESS()
        }else{
            ResultData.FAILED("请检查参数")
        }
    }

    @DeleteMapping
    @ApiOperation("根据id删除")
    @ValidateAspect.ParamValidate
    fun deleteById(id: Long): ResultData<Any>{
        val res = goodsTypeService.deleteById(id)
        return if(res){
            ResultData.SUCCESS()
        }else{
            ResultData.FAILED("该type下仍有goods，无法删除")
        }
    }

}

